﻿using System;
using PostSharp.Laos;
using TestBat.IModels.Services;
using TestBat.IViewModels.Dialogs;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Base.Commands;
using TestBat.ViewModels.Dialogs;

namespace TestBat.ViewModels.Attributes
{
    [Serializable]
    public class CheckUnsavedChangesAttribute : OnMethodInvocationAspect
    {
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            // get current session
            IUnsavedChangesService ucs = DefaultServiceLocator.Get<IUnsavedChangesService>();

            if (ucs.UnsavedChanges())
            {
                DefaultServiceLocator.Get<IDialogMediator>().DialogViewModel = new UnsavedChangesDialogViewModel(new RelayCommand(e => eventArgs.Proceed()), new RelayCommand(e => { }));
            }
            else
                eventArgs.Proceed();
        }
    }
}
